package schedule

import (
	"fmt"
	"time"

	. "gddgame.cc/galaxy/common/dsl/web"
	"gddgame.cc/galaxy/utils/def"
)

func GetScheduleAll(context def.Context, server string) ([]ServerSchedule, error) {
	var scheduleList []ServerSchedule
	query := ServerScheduleModel.Query()
	if server != "" {
		query.WhereSql("`server` like ?", "%"+server+"%")
	}
	query.Where("=", "status", 1)
	if _, err := query.Find(&scheduleList); err != nil {
		return nil, SystemException(err)
	}
	return scheduleList, nil
}

func ErrorSchedule(context def.Context, server string, cron string, command string, err error) error {
	_, err = ServerScheduleModel.Query().Where("=", "server", server).Where("=", "cron", cron).Where("=", "command", command).Update(map[string]interface{}{
		"fail": fmt.Sprintf("%s : %s", time.Now().String(), err.Error()),
	})
	if err != nil {
		return SystemExceptionDetail("更新失败", err)
	}
	return nil
}
